//meta variables
//this file couldn't include any clause or function depends on jQuery or other libs.
var ds_Product = '屠龍者';
var ds_CodeName = 'Wave VIP';
var ds_Version = '正式版 1.3 090408';

var CONFIG = {
    hunt_radius: 33,
    fix_radius: true,
    attack_wild: true,
    attack_evilCity: true,
    attack_wheel: true,
    auto_gamble: true,
    auto_resurrect: false,
	auto_read:'true',//自動閱讀短消息
    enable_armyRules: false,
    enable_armyRulesAverage: true,
    army_rules: [], //{id:"1111",heroName:"亏托斯",armyType:"鬼魂",armyCount:100}
    enable_autoSellResource: false,
    resource_sellAmount: 50000,
    enable_mapLog: false,
    enable_attackDelay: false,
    enable_huntRules: false,
    hunt_rules: [], //{id:"1111",heroName:"苦托斯",radius:2,fixRadius:false,target:"wild/devil/wheel",attackType:"attack/collect",blood:"50/30/10",herotodo:"1/2/3",Treatment:"1/2/3/5/8/12/24/48"}
    enable_multiCity: true,
    ignore_cityRules: [], //{id:"1111",cityName:"屠龙者主城"}
    enable_buildingRules: false,
    building_rules: [], //{id:"1111",cityName:"屠龙者主城",buildingName:"军事指挥所"}
    attackList: [], //{id:"1111",names:"闹鬼(-231 | 294)",urls:"/a2t?type=2&mapId=2700795"}
	messageoption:"3",
    market_rules: {
        decisionpercent: 0.3,
        isautobuy: false,
        woodbuyprice: "",
        stonebuyprice: "",
        irishbuyprice: "",
        foodbuyprice: "",
        isautosale: false,
        woodsaleprice: "",
        stonesaleprice: "",
        irishsaleprice: "",
        foodsaleprice: ""
    }
};


// identify for different page,different state for state machine,for different state ,logic code will load different processing logic code by add <script> tag
// based on finite state machine , every set for window.location.href is a state transition
var states = [{
    name: 'mindex', //unique name to find the process handler
    identity: '/mindex', //page identity
    text: '首頁', // text for human understanding
    configObj: {} // some configuration varibles
}, {
    name: 'building', //unique name to find the process handler
    identity: '/building', //page identity
    text: '建築頁', // text for human understanding
    configObj: {} // some configuration varibles
}, {
    name: 'GameMap',
    identity: '/GameMap',
    text: '地圖頁',
    configObj: {
        areaRadius: 3
    }
}, {
    name: 'GameMapInfo',
    identity: '/GameMapInfo',
    text: '地圖信息頁',
    configObj: {}
}, {
    name: 'a2t',
    identity: '/a2t',
    text: '出軍頁面',
    configObj: {}
}, {
    name: 'gdpf',
    identity: '/gdpf',
    text: '酒館賭博頁面',
    configObj: {}
}, {
    name: 'resourcesell',
    identity: '/resourcesell',
    text: '賣資源給NPC頁面',
    configObj: {}
}, {
    name: 's2h',
    identity: '/s2h',
    text: '英雄分兵頁面',
    configObj: {}
}, {
    name: 'messageList',
    identity: '/messageList',
    text: '消息頁面',
    configObj: {}
}, {
    name: 'market',
    identity: '/market',
    text: '資源市場頁面',
    configObj: {}
}, {
    name: 'MarketForm',
    identity: '/MarketForm',
    text: '交易成功頁面',
    configObj: {}
},{
	name: 'nbl',
	identity:'/nbl',
	text: '未建筑列表',
	configObj:{}
},{
	name: 'hero',
	indetity:'/hero',
	text:'英雄屬性頁面',
}];


//utility functions,also treat as an common function library.
function $ID(id){
    return document.getElementById(id);
}

//xpath helper function
function $x(p, context){
    if (!context) 
        context = document;
    var i, arr = [], xpr = document.evaluate(p, context, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    for (i = 0; item = xpr.snapshotItem(i); i++) 
        arr.push(item);
    return arr;
}

//extend object function
function extend(tar, source){
    for (var key in source) {
        tar[key] = source[key];
    }
    return tar;
}

//simulate the human click the link
function clickLink(link){
    var randomTime = Math.random() * 15000;
    randomTime = randomTime < 3000 ? 3000 : randomTime;
    var str = "window.location.href = '" + link + "'";
    window.setTimeout(str, randomTime);
}

//function to generate the action probabilty
//probability between 0 - 1
function makeDecision(probability){
    var random = Math.random();
    if (random <= probability) 
        return true;
    return false;
}

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to){
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};
Array.prototype.removeByObjectProperty = function(prop, val){
    var rmIdx = -1;
    for (var i = 0; i < this.length; i++) {
        if (this[i][prop] == val) {
            rmIdx = i;
            break;
        }
    }
    if (rmIdx != -1) 
        return this.remove(rmIdx);
    return this;
}
//getObject by object property from an objects array
Array.prototype.getObjectByProperty = function(property, value){
    for (var i = 0; i < this.length; i++) {
        if (this[i][property] == value) 
            return this[i];
    }
    return null;
}
Array.prototype.exists = function(value){
    for (var i = 0; i < this.length; i++) {
        if (this[i] == value) 
            return i;
    }
    return -1;
}
String.prototype.trim = function(){
    return this.replace(/^\s+|\s+$/g, '');
}

String.prototype.startWith=function(str){
    if(str==null||str==""||this.length==0||str.length>this.length)
        return false;
    if(this.substr(0,str.length)==str)
        return true;
    else
        return false;
    return true;
}
String.prototype.trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
